package com.dalao.root.custom.controller;

import com.dalao.root.custom.entity.CusField;
import com.dalao.root.custom.entity.CusForm;
import com.dalao.root.custom.entity.CusTable;
import com.dalao.root.custom.service.CustomService;
import com.dalao.root.custom.util.CustomBox;
import com.dalao.root.system.controller.BaseController;
import com.dalao.root.system.entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("custom")
public class CustomController extends BaseController{

    @Autowired
    private CustomService customService;

    @PostMapping("/table/create")
    public Result newTable(@RequestBody CusTable table){
        result = new Result();
        CustomBox customBox = new CustomBox();
        if(!customBox.checkTable(table)) {
            result.fail(customBox.getMessage());
        }else {
            CusTable cusTable = customService.newTable(table);
            result.setData(cusTable);
            if(cusTable!=null){
                result.success("创建表" + table.getName() + "成功");
            }else {
                result.fail("创建表失败");
            }
        }
        return result;
    }

    @GetMapping("/table/drop")
    public Result dropTable(CusTable table){
        result = new Result();
        CusForm cusForm = customService.findForm(table);
        if(table.getId()==null) {
            result.fail("id不能为空");
        }else {
            if(cusForm.getFields().size()>0){
                for(CusField cusField : cusForm.getFields()) {
                    customService.dropField(cusField);
                }
            }
            customService.dropTable(table);
            result.success("删除成功");
        }
        return result;
    }

    @PostMapping("/column/add")
    public Result addColumn(@RequestBody CusField cusField){
        result = new Result();
        try {
            cusField = customService.addField(cusField);
            if(cusField!=null && cusField.getId()!=null){
                result.success("添加成功");
                result.setData(cusField);
            }else{
                result.fail("添加失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    @GetMapping("/column/delete")
    public Result deleteColumn(CusField cusField){
        result = new Result();
        try {
            if(customService.dropField(cusField)){
                result.success("删除成功");
            }else{
                result.fail("删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @PostMapping("/data/insert/{table}")
    public Result insertData(@PathVariable("table") String table,@RequestBody Map<String,String> data){
        result = new Result();
        try {
            CusTable cusTable = new CusTable();
            cusTable.setName(table);
            CusForm cusForm = customService.findForm(cusTable);

            Map<String,String> tableData = customBox.tableData(data,cusForm.getFields());

            Integer id = customService.insertData(cusForm.getName(),tableData);
            if(id!=null) {
                result.success();
                result.setData(id);
            }else{
                result.fail();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @PostMapping("/data/delete/{table}")
    public Result deleteData(@PathVariable("table") String table,@RequestBody List<Map<String,String>> where){
        result = new Result();
        try {
            CusTable cusTable = new CusTable();
            cusTable.setName(table);
            CusForm cusForm = customService.findForm(cusTable);
            where = customBox.whereData(where,cusForm.getFields());
            if(customService.deleteData(cusForm.getName(),where)) {
                result.success("删除成功");
            }else{
                result.fail("删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.fail(e.getMessage());
        }
        return result;
    }

    @PostMapping("/data/update/{table}")
    public Result updateData(@PathVariable("table") String table,@RequestBody Map<String,Object> formData){
        result = new Result();
        try {
            CusTable cusTable = new CusTable();
            cusTable.setName(table);
            CusForm cusForm = customService.findForm(cusTable);

            Map<String,String> data = (Map<String,String>)formData.get("field");
            List<Map<String,String>> wheres = (List<Map<String,String>>)formData.get("where");
            //where属性名称转换
            wheres = customBox.whereData(wheres,cusForm.getFields());

            Map<String,String> tableData = customBox.tableData(data,cusForm.getFields());

            if(customService.updateData(cusForm.getName(),tableData,wheres)) {
                result.success("修改成功");
            }
            else {
                result.fail("修改失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    @PostMapping("/data/select/{table}")
    public Result findData(@PathVariable("table") String table,@RequestBody List<Map<String,String>> where){
        result = new Result();
        try {
            CusTable cusTable = new CusTable();
            cusTable.setName(table);
            CusForm cusForm = customService.findForm(cusTable);
            where = customBox.whereData(where,cusForm.getFields());
            Object data = customService.selectData(cusForm.getName(),cusForm.getFields(),where);
            result.success();
            result.setData(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @PostMapping("/data/list/{table}")
    public Result findDataList(@PathVariable("table") String table,@RequestBody List<Map<String,String>> where){
        result = new Result();
        try {
            CusTable cusTable = new CusTable();
            cusTable.setName(table);
            CusForm cusForm = customService.findForm(cusTable);
            where = customBox.whereData(where,cusForm.getFields());
            Object data = customService.selectDataList(cusForm.getName(),cusForm.getFields(),where);
            result.success();
            result.setData(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

}
